package org.dromara.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.SysMemberGroup;
import org.dromara.system.domain.bo.SysMemberGroupBo;
import org.dromara.system.domain.vo.SysMemberGroupVo;
import org.dromara.system.domain.vo.SysUserFreezePointsRecordsVo;
import org.dromara.system.enums.SysUserFreezePointsRecordsEnum;
import org.dromara.system.mapper.SysMemberGroupMapper;
import org.dromara.system.service.ISysMemberGroupService;
import org.dromara.system.service.impl.permission.PermissionBaseService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 会员分组Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-18
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SysMemberGroupServiceImpl extends ServiceImpl<SysMemberGroupMapper, SysMemberGroup> implements ISysMemberGroupService {


    private final PermissionBaseService permissionBaseService;

    /**
     * 查询会员分组
     *
     * @param id 主键
     * @return 会员分组
     */
    @Override
    public SysMemberGroupVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询会员分组列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 会员分组分页列表
     */
    @Override
    public TableDataInfo<SysMemberGroupVo> queryPageList(SysMemberGroupBo bo, PageQuery pageQuery) {
        permissionBaseService.assignPermission(bo);
        IPage<SysMemberGroupVo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        IPage<SysMemberGroupVo> list = baseMapper.findAll(page, bo);
        return TableDataInfo.build(list);
    }

    /**
     * 查询符合条件的会员分组列表
     *
     * @param bo 查询条件
     * @return 会员分组列表
     */
    @Override
    public List<SysMemberGroupVo> queryList(SysMemberGroupBo bo) {
        LambdaQueryWrapper<SysMemberGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysMemberGroup> buildQueryWrapper(SysMemberGroupBo bo) {
        LambdaQueryWrapper<SysMemberGroup> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(SysMemberGroup::getId);
        lqw.eq(bo.getUserId() != null, SysMemberGroup::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getMemberGroupName()), SysMemberGroup::getMemberGroupName, bo.getMemberGroupName());
        lqw.eq(bo.getComplianceRewards() != null, SysMemberGroup::getComplianceRewards, bo.getComplianceRewards());
        lqw.eq(bo.getComplianceCheckAmount() != null, SysMemberGroup::getComplianceCheckAmount, bo.getComplianceCheckAmount());
        lqw.eq(bo.getComplianceCheckMonth() != null, SysMemberGroup::getComplianceCheckMonth, bo.getComplianceCheckMonth());
        lqw.eq(StringUtils.isNotBlank(bo.getAfterSaleWaiter()), SysMemberGroup::getAfterSaleWaiter, bo.getAfterSaleWaiter());
        lqw.eq(StringUtils.isNotBlank(bo.getAfterSalesRepair()), SysMemberGroup::getAfterSalesRepair, bo.getAfterSalesRepair());
        lqw.eq(StringUtils.isNotBlank(bo.getAfterSalesWechat()), SysMemberGroup::getAfterSalesWechat, bo.getAfterSalesWechat());
        lqw.eq(bo.getStatus() != null, SysMemberGroup::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderTitle()), SysMemberGroup::getOrderTitle, bo.getOrderTitle());
        lqw.eq(bo.getOrderPrice() != null, SysMemberGroup::getOrderPrice, bo.getOrderPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderDescription()), SysMemberGroup::getOrderDescription, bo.getOrderDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadquartersAddress()), SysMemberGroup::getHeadquartersAddress, bo.getHeadquartersAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getAfterSalesAddress()), SysMemberGroup::getAfterSalesAddress, bo.getAfterSalesAddress());
        return lqw;
    }

    /**
     * 新增会员分组
     *
     * @param bo 会员分组
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SysMemberGroupBo bo) {
        if (ObjectUtil.isNull(bo.getUserId())){
            bo.setUserId(LoginHelper.getUserId());
        }
        SysMemberGroup add = MapstructUtils.convert(bo, SysMemberGroup.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改会员分组
     *
     * @param bo 会员分组
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SysMemberGroupBo bo) {
        SysMemberGroup update = MapstructUtils.convert(bo, SysMemberGroup.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysMemberGroup entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除会员分组信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
